package com.tempo.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

/**
 * @author tempo
 *         on 2015/11/23.
 */
public abstract class LoadingView<T> extends FrameLayout {
    public enum LoadingState {
        Loading, Empty, Error, Success
    }
    public LoadingState loadState = null;
    public ResultState resState;
    public LoadingView(Context context) {
        super(context);
        init(context);
    }

    /**
     *start loading data
     */
    public void startLoad(){
        switchState(LoadingState.Loading);
    }
    protected void init(Context context) {
        switchState(LoadingState.Loading);
    }

    protected void switchState(LoadingState state) {
      switchState(state,null);
    }
    protected void switchState(LoadingState state,T data){
        View target = null;
        switch (state) {
            case Loading:
                target = getLoadingView(getContext());
                break;
            case Empty:
                target = getEmptyView(getContext());
                break;
            case Error:
                target = getErrorView(getContext());
                break;
            case Success:
                target = getSuccessView(getContext(),data);
                break;
            default:
                throw new IllegalArgumentException("undefined type");
        }
        loadState = state;
        removeAllViews();
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        target.setLayoutParams(params);
        addView(target);
        if(state==LoadingState.Loading){
            loadData(new LoadingCallback());
        }

    }

    protected abstract View getSuccessView(Context context,T data);

    protected View getErrorView(Context context) {
        View errorView = LayoutInflater.from(context).inflate(R.layout.loading_error, null);
        errorView.findViewById(R.id.btn_loadmore).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switchState(LoadingState.Loading);
            }
        });
        return errorView;
    }

    protected View getLoadingView(Context context) {
        View loadView = LayoutInflater.from(context).inflate(R.layout.loading_loading, null);
        return loadView;
    }

    protected View getEmptyView(Context context) {
        View empty = LayoutInflater.from(context).inflate(R.layout.loading_empty, null);
        return empty;
    }
    protected abstract void loadData(LoadingCallback callback);
    public class LoadingCallback{
       public void setResult(ResultState resultState, T data){
           /**
            * 之前有加载到数据,现在加载失败,保留之前的页面
            */
           if(resultState!=ResultState.Success&&resState==ResultState.Success){
               return;
           }
           switch (resultState){
               case Success:
                   switchState(LoadingState.Success,data);;break;
               case Error :
                   switchState(LoadingState.Error);break;
               case Empty :
                   switchState(LoadingState.Empty);break;
           }
           resState=resultState;

       }
     }
    public enum ResultState{
        Success,Error,Empty
    }
}
